package controllers

import (
	"encoding/json"
	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
)

type PrescController struct {
	BaseController
}

func (c *PrescController) Prepare() {
	//先执行
	c.BaseController.Prepare()
	c.Data["activeSidebarUrl"] = "/presc/index"
}

//开具处方
func (c *PrescController) Index() {
	c.checkAuthor("presc/index")
	if c.Ctx.Input.IsPost() {
		query := models.DoGuahaoQueryParam{}
		json.Unmarshal(c.Ctx.Input.RequestBody, &query)
		query.DepartmentId = c.curUser.DepartmentId
		query.Status = []int{0, 1}
		query.Sort = "status"
		query.Order = "asc"
		list, count := models.DoGuahaoPageList(&query)
		c.jsonDataTable(count, list)
	}
	c.display("", "presc/index_footerjs.html")
}

//开处方
func (c *PrescController) Prescription() {
	c.checkAuthor("presc/index")

	c.display("", "presc/prescription_footerjs.html", "shared/layout_dialog.html")
}

//保存处方
func (c *PrescController) SavePrescription() {
	c.checkAuthor("presc/index")

	archives_id, _ := c.GetInt64("archives_id")
	guahao_id, _ := c.GetInt64("guahao_id")
	if archives_id == 0 || guahao_id == 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	save_data := make([]*models.DoDoctorOrder, 0)
	save_datas := c.GetString("save_datas")
	if len(save_datas) <= 0 {
		c.jsonResult(enums.JRCodeFailed, "请填写医嘱内容", "")
	}
	if err := json.Unmarshal([]byte(save_datas), &save_data); err != nil {
		c.jsonResult(enums.JRCodeFailed, "请填写医嘱内容", "")
	}
	diagnosis := c.GetString("diagnosis")
	if err := models.SaveDoDoctorOrder(c.curUser, archives_id, guahao_id, save_data, diagnosis); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//调取诊断
func(c *PrescController) Diagnosis() {
	guahao_id,_ := c.GetInt64("guahao_id")
	archives_id, _ := c.GetInt64("archives_id")
	diags := models.DoDiagnosisQuery(archives_id, guahao_id)
	if diags.Id == 0 {
		c.jsonResult(enums.JRCodeFailed, "没有诊断", "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", diags)
}

//调取常用项目
func (c *PrescController) OftenProject() {
	project_type := c.GetString("project_type")
	param := new(models.DoProjectOftenQueryParam)
	param.ProjectType = project_type
	param.DepartmentId = c.curUser.DepartmentId
	lists := models.DoProjectOftenPageList(param)
	c.jsonResult(enums.JRCodeSucc, "ok", lists)
}

//查询处方列表
func(c *PrescController) DoctorOrderQuery() {
	query := &models.DoDoctorOrderQueryParam{}
	json.Unmarshal(c.Ctx.Input.RequestBody, &query)
	query.Department = c.curUser.DepartmentId
	do := models.DoDoctorOrderList(query)
	c.jsonResult(enums.JRCodeSucc, "ok", do)
}

//处方操作
func(c *PrescController) DoctorOrderAction() {
	act_type,_:= c.GetInt("act_type")
	id,_:= c.GetInt64("id")
	if act_type == 0 {
		c.jsonResult(enums.JRCodeFailed, "操作类型错误", "")
	}
	if id == 0 {
		c.jsonResult(enums.JRCodeFailed, "医嘱参数错误", "")
	}
	if err := models.DoctorOrderActionStatus(act_type, id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}